package com.zxx.mvpPractice.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.google.common.base.Preconditions;
import com.zxx.mvpPractice.base.RxPresenter;
import com.zxx.mvpPractice.model.bean.RequestSmsBody;
import com.zxx.mvpPractice.model.bean.User;
import com.zxx.mvpPractice.model.net.RetrofitHelper;
import com.zxx.mvpPractice.model.net.bean.LoginRegisterReponse;
import com.zxx.mvpPractice.model.net.bean.SmsResponse;
import com.zxx.mvpPractice.model.net.bean.VerifyResponse;
import com.zxx.mvpPractice.presenter.contract.LoginFragmentContract;
import com.zxx.mvpPractice.utils.RxManagerUtils;
import com.zxx.mvpPractice.utils.callback.RxCallback;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/12/17.
 */
public class LoginFragmentPresenter extends RxPresenter implements LoginFragmentContract.Presenter {
    LoginFragmentContract.View mView;

    public LoginFragmentPresenter(@NonNull LoginFragmentContract.View oneView) {
        mView = Preconditions.checkNotNull(oneView);
        mView.setPresenter(this);
    }


    @Override
    public void bombUserLogin(String username, String password, int loginMode) {
        if (loginMode == 0) {
            loginBySms(username, password);
        }
        if (loginMode == 1) {
            loginByPwd(username, password);
        }
    }

    @Override
    public void bombRequestSms(String phoneNumber) {

        if (TextUtils.isEmpty(phoneNumber)) {
            mView.showMessage("手机号不能为空");
            return;
        }

        RequestSmsBody requestSmsBody = new RequestSmsBody();
        requestSmsBody.setMobilePhoneNumber(phoneNumber);
        Observable<SmsResponse> smsResponseObservable = RetrofitHelper.getBombLoinApi().requestSmsCode(requestSmsBody);

        RxManagerUtils.getInstance().commSubscribe(smsResponseObservable, new RxCallback() {
            @Override
            public void onStart() {
                mView.showProgressDialog();
            }

            @Override
            public void onCompleted() {
                mView.hideProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                Log.d("zxx", "showErrorMessage:" + e.getMessage());
                mView.showMessage("发送失败");
                mView.hideProgressDialog();
            }

            @Override
            public void onNext(Object smsResponse) {
                mView.showMessage("发送成功");
                Log.d("zxx", "onNext:" + ((RequestSmsBody) smsResponse).toString());
            }
        });
    }

    @Override
    public void verifySms() {
        String smsCode = "";
        String phoneNumber = "";
        RetrofitHelper.getBombLoinApi().verifySms(smsCode, phoneNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<VerifyResponse>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("验证失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(VerifyResponse msg) {
//                        mView.showBmob(msg);
                        mView.showMessage("验证成功");
                        Log.d("zxx", "onNext:" + msg.toString());

                    }
                });
    }

    private void loginBySms(String username, String password) {
        if (TextUtils.isEmpty(username)) {
            mView.showMessage("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            mView.showMessage("验证码不能为空");
            return;
        }

        User user = new User();
        user.setMobilePhoneNumber(username);
        user.setMobilePhoneNumberVerified(true);
        RetrofitHelper.getBombLoinApi().loginOrRegisterUser(user)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginRegisterReponse>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("注册失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(LoginRegisterReponse msg) {
//                        mView.showBmob(msg);
                        mView.showMessage("注册成功");
                        Log.d("zxx", "onNext:" + msg.toString());
                    }
                });
    }

    private void loginByPwd(String username, String password) {
        if (TextUtils.isEmpty(username)) {
            mView.showMessage("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            mView.showMessage("密码不能为空");
            return;
        }
        Map<String, String> options = new HashMap<String, String>();
        options.put("mobilePhoneNumber", username);
        options.put("password", password);
        options.put("email", "563487347@qq.com");

        Observable<LoginRegisterReponse> loginRegisterReponseObservable = RetrofitHelper.getBombLoinApi().loginUser(options);
        RxManagerUtils.getInstance().commSubscribe(loginRegisterReponseObservable, new RxCallback() {
            @Override
            public void onStart() {
                mView.showProgressDialog();
            }

            @Override
            public void onCompleted() {
                mView.hideProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.showMessage("登录失败");
            }

            @Override
            public void onNext(Object response) {
                mView.showMessage("登录成功");
                mView.LoginSuccess();
                Log.d("zxx", "onNext:" + ((LoginRegisterReponse) response).toString());
            }
        });


//        .subscribeOn(Schedulers.io())
//                .unsubscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<LoginRegisterReponse>() {
//                    @Override
//                    public void onStart() {  //先显示对话框
//                        mView.showProgressDialog();
//                    }
//
//                    @Override
//                    public void onCompleted() {  //请求结束，对话框消失
//                        mView.hideProgressDialog();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {   //error时
//                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
//                        mView.showMessage("登录失败");
//                    }
//
//                    @Override
//                    public void onNext(LoginRegisterReponse msg) {
////                        mView.showBmob(msg);
//                        mView.showMessage("登录成功");
//                        mView.LoginSuccess();
//                        Log.d("zxx", "onNext:" + msg.toString());
//                    }
//                });
    }
}
